import pygame as pyg
from pygame import mixer

import logging

import settings as st
import runtime
from control import control_proxy as ctl_proxy
from control.task import Task, TASK_ACTION
from uart import request


logger = logging.getLogger(__name__)


# 初始化，采样率设为 16 kHz（音频文件的采样率）
mixer.init(frequency=16000)


# 加载声音
# 扫描完成, 请装入 * 号物料, 并关闭仓门
snd_ld = {
    1: mixer.Sound(st.PTH_SND_LD_1),
    2: mixer.Sound(st.PTH_SND_LD_2)
}

# 到达 * 号收货点, 请初始提货码
snd_arv = {
    1: mixer.Sound(st.PTH_SND_ARV_1),
    2: mixer.Sound(st.PTH_SND_ARV_2)
}

# 请取出 * 号物料, 并关闭仓门
snd_uld = {
    1: mixer.Sound(st.PTH_SND_ULD_1),
    2: mixer.Sound(st.PTH_SND_ULD_2)
}

# 取货码错误
snd_others = {
    st.V_SND_WRONG_CODE: mixer.Sound(st.PTH_SND_WRG_CODE)
}


sounds = {
    st.V_SND_LOAD: snd_ld,
    st.V_SND_ARRIVE: snd_arv,
    st.V_SND_UNLOAD: snd_uld,
}



class TaskSound(Task):
    def __init__(self):
        Task.__init__(self, task_type=TASK_ACTION)
    
    def work(self):
        cmd, values = request.dataframe
        step = values[0] if values else None
        
        if cmd != st.LB_SND:
            logger.info('sound no played, wrong label')
            
        elif step == st.V_SND_LOAD or step == st.V_SND_UNLOAD:
            if runtime.num_stuff in snd_ld.keys():
                sounds[step][runtime.num_stuff].play()
                return st.LB_OK, []
            else:
                logger.info('sound no played, because num_stuff not known')
        
        elif step == st.V_SND_ARRIVE:
            if runtime.num_dst in snd_arv.keys():
                sounds[step][runtime.num_dst].play()
                return st.LB_OK, []
            else:
                logger.info('sound no played, because num_dst not known')

        elif step in snd_others.keys():
            snd_others[step].play()
            return st.LB_OK, []
        else:
            logger.info('sound no played, unknown step')
        
        return st.LB_ERR, []


music_water = pyg.mixer.music.load(st.PTH_MUSIC_WWW)
def play_water():
    pyg.mixer.music.play()


def stop_water():
    pyg.mixer.music.stop()


if __name__ == '__main__':
    duration = 5000  # ms, length of the sound
    
    tk = TaskSound()
    
    print()
    print('='*30)
    print('testing...')
    print('wrong label')
    request.set_dataframe(('aaa', []))
    tk.work()
    
    print()
    print('QR no scaned (play sound before scan QR code)')
    request.set_dataframe((st.LB_SND, [st.V_SND_LOAD]))
    tk.work()
    request.set_dataframe((st.LB_SND, [st.V_SND_ARRIVE]))
    tk.work()
    request.set_dataframe((st.LB_SND, [st.V_SND_UNLOAD]))
    tk.work()
    
    print()
    print('wrong stuff number')
    request.set_dataframe((st.LB_SND, [st.V_SND_LOAD]))
    runtime.num_stuff = 10
    tk.work()
    
    
    print()
    print('all inputs are right, test if the corrent sound is played corresponsdly.')
    print('load 1')
    request.set_dataframe((st.LB_SND, [st.V_SND_LOAD]))
    runtime.num_stuff = 1
    tk.work()
    pyg.time.delay(duration)
    
    print('load 2')
    request.set_dataframe((st.LB_SND, [st.V_SND_LOAD]))
    runtime.num_stuff = 2
    tk.work()
    pyg.time.delay(duration)
    
    print('arrive 1')
    request.set_dataframe((st.LB_SND, [st.V_SND_ARRIVE]))
    runtime.num_dst = 1
    tk.work()
    pyg.time.delay(duration)
    
    print('arrive 2')
    request.set_dataframe((st.LB_SND, [st.V_SND_ARRIVE]))
    runtime.num_dst = 2
    tk.work()
    pyg.time.delay(duration)
    
    print('unload 1')
    request.set_dataframe((st.LB_SND, [st.V_SND_UNLOAD]))
    runtime.num_stuff = 1
    tk.work()
    pyg.time.delay(duration)
    
    print('unload 2')
    request.set_dataframe((st.LB_SND, [st.V_SND_UNLOAD]))
    runtime.num_stuff = 2
    tk.work()
    pyg.time.delay(duration)
    
    print('wrong code')
    request.set_dataframe((st.LB_SND, [st.V_SND_WRONG_CODE]))
    tk.work()
    pyg.time.delay(duration)
    
    